<?php
	require_once( 'core_DataBaseConnect.php' );
	require_once( '_configs.php' );
	
	class MemberModelDAO
	{
		// 멤버가 부여하는 카테고리 업데이트
		public static function updateModelStat( DataBaseConnect $con, 
													$login_member_id, 
													$modelCode, 
													&$statCodes, 
													&$statValues )
		{
			while( count( $statCodes ) > 0 )
			{
				$code = array_shift( $statCodes );
				$value = array_shift( $statValues );
				
				$query = 'SELECT COUNT(SEQ) FROM '.configs::TABLE_MEMBERMODELSTAT." WHERE `M_CODE`='$modelCode' AND `S_CODE`='$code' AND `OWNER_ID`='$login_member_id' ;";
				$fetch = DataBaseConnect::fetch_assoc( $con->query( $query ) );
				
				if( $fetch[ 'COUNT(SEQ)' ] != '0' )
				{
					// 업데이트
					$query = 'UPDATE '.configs::TABLE_MEMBERMODELSTAT." SET `POINT`='$value' WHERE `M_CODE`='$modelCode' AND `S_CODE`='$code' AND `OWNER_ID`='$login_member_id' ;";
				}else{
					// 추가
					$query = 'INSERT INTO '.configs::TABLE_MEMBERMODELSTAT.
							"(
								`SEQ` ,
								`M_CODE` ,
								`S_CODE` ,
								`POINT`,
								`OWNER_ID`
							)
							VALUES 
							(
								NULL, 
								'$modelCode',
								'$code',
								'$value',
								'$login_member_id'
							);";
				}
				$con->query( $query );
			}
		}
		
		// 멤버가 부여한 능력치 조회
		public static function selectModelStat( DataBaseConnect $con, 
													$login_member_id, 
													$modelCode )
		{
			$query = 'SELECT * FROM '.configs::TABLE_MEMBERMODELSTAT." WHERE `M_CODE`='$modelCode' AND `OWNER_ID`='$login_member_id';";
			return $con->query( $query );
		}
		
		// 멤버가 모델을 즐겨찾기에 등록했는지 여부
		public static function isModelFavorite( DataBaseConnect $con, 
													$login_member_id, 
													$modelCode )
		{
			$query = 'SELECT COUNT(SEQ) FROM '.configs::TABLE_MEMBERMODELFAVORITES." WHERE `M_CODE`='$modelCode' AND `OWNER_ID`='$login_member_id';";
			$fetch = DataBaseConnect::fetch_assoc( $con->query( $query ) );
			return $fetch[ 'COUNT(SEQ)' ]=='1';
		}
		
		
		// 멤버가 모델을 즐겨찾기에 등록
		public static function insertModelFavorite( DataBaseConnect $con, 
													$login_member_id, 
													$modelCode )
		{
			$query = 'SELECT COUNT(SEQ) FROM '.configs::TABLE_MEMBERMODELFAVORITES." WHERE `M_CODE`='$modelCode' AND `OWNER_ID`='$login_member_id';";
			$fetch = DataBaseConnect::fetch_assoc( $con->query( $query ) );
			if( (int)$fecth[ 'COUNT(SEQ)' ] >= 1 ) return;
			
			$query ='INSERT INTO '.configs::TABLE_MEMBERMODELFAVORITES.
						"(
							`M_CODE`,
							`OWNER_ID`
						)
						VALUES
						(
							'$modelCode',
							'$login_member_id'
						);";
			return $con->query( $query );
		}
		
		// 멤버가 모델을 즐겨찾기에서 해제
		public static function deleteModelFavorite( DataBaseConnect $con, 
													$login_member_id, 
													$modelCode )
		{
			$query = 'DELETE FROM '.configs::TABLE_MEMBERMODELFAVORITES." WHERE `M_CODE`='$modelCode' AND `OWNER_ID`='$login_member_id';";
			return $con->query( $query );
		}
		
		
		// 멤버가 모델의 댓글을 등록
		public static function insertModelComment( DataBaseConnect $con, 
													$login_member_id, 
													$modelCode,
													$comment )
		{
			$query = 'INSERT INTO '.configs::TABLE_MEMBERMODELCOMMENTS.
						"(
							`M_CODE`,
							`OWNER_ID`,
							`COMMENT`,
							`REGISTER`
						)VALUES(
							'$modelCode',
							'$login_member_id',
							'$comment',
							NOW()
						);";
			return $con->query( $query );
		}
		
		// 모든 댓글 수 가져오기
		public static function countModelComments( DataBaseConnect $con,
													$modelCode )
		{
			$query = 'SELECT COUNT(SEQ) FROM '.configs::TABLE_MEMBERMODELCOMMENTS." WHERE `M_CODE`='$modelCode';";
			$fetch = DataBaseConnect::fetch_assoc( $con->query( $query ) );
			return $fetch[ 'COUNT(SEQ)' ];
		}
		
		// 모든 댓글 목록 가져오기
		public static function selectModelComments( DataBaseConnect $con,
												$modelCode,
												$page,
												$numPerPage )
		{
			$startIndex = max( ($page-1)*$numPerPage, 0 );
			$query = 'SELECT t1.*, t2.NICKNAME 
							FROM '.configs::TABLE_MEMBERMODELCOMMENTS.' AS t1 
							JOIN '.configs::TABLE_MEMBER." AS t2 
							ON t1.OWNER_ID = t2.SEQ
							WHERE 
								`M_CODE`='$modelCode' 
							LIMIT 
								$startIndex,$numPerPage;";
								
			return $con->query( $query );
		}
		
		
		// 댓글 삭제하기
		public static function deleteModelComment( DataBaseConnect $con,
												$seq,
												$login_member_id)
		{
			$query = 'DELETE FROM '.configs::TABLE_MEMBERMODELCOMMENTS." WHERE `SEQ`='$seq' AND `OWNER_ID`='$login_member_id' LIMIT 1;;";
			$con->query( $query );
			return ( $con->affected_rows() > 0 );
		}
		
		// 특정 코멘트를 조회함
		public static function selectModelCommentBySeq( DataBaseConnect $con, $seq )
		{
			$query = 'SELECT * FROM '.configs::TABLE_MEMBERMODELCOMMENTS." WHERE `SEQ`='$seq' LIMIT 1;";
			return $con->query( $query );
		}
		
		// 즐겨찾기 등록된 목록 수
		public static function countFavoriteModels( DataBaseConnect $con, $login_member_id )
		{
			$query = 'SELECT COUNT(SEQ) FROM '.configs::TABLE_MEMBERMODELFAVORITES."
						WHERE
							`OWNER_ID`='$login_member_id'
						LIMIT 1;";
			$fetch = DataBaseConnect::fetch_assoc( $con->query( $query ) );
			return $fetch[ 'COUNT(SEQ)' ];
		}
		// 즐겨찾기 등록된 목록 조회
		public static function selectFavoriteModels( DataBaseConnect $con, $login_member_id, $page, $numPerPage )
		{
			$startIndex = max( ($page-1)*$numPerPage, 0 );
			
			$query = 'SELECT T2.* FROM '.configs::TABLE_MEMBERMODELFAVORITES.' AS T1 JOIN '.configs::TABLE_MODEL." AS T2
						ON
							T1.M_CODE = T2.CODE
						WHERE
							T1.OWNER_ID = '$login_member_id'
						LIMIT
							$startIndex,$numPerPage;";
			return $con->query( $query );
		}
		
		
		// 댓글 남긴 목록 수
		public static function countCommentModels( DataBaseConnect $con, $login_member_id )
		{
			$query = 'SELECT COUNT(Distinct `M_CODE`) FROM '.configs::TABLE_MEMBERMODELCOMMENTS."
						WHERE
							`OWNER_ID`='$login_member_id'
						LIMIT 1;";
			$fetch = DataBaseConnect::fetch_assoc( $con->query( $query ) );
			return $fetch[ 'COUNT(Distinct `M_CODE`)' ];
		}
		// 즐겨찾기 등록된 목록 조회
		public static function selectCommentModels( DataBaseConnect $con, $login_member_id, $page, $numPerPage )
		{
			$startIndex = max( ($page-1)*$numPerPage, 0 );
			
			$query = 'SELECT Distinct T1.M_CODE,T2.* FROM '.configs::TABLE_MEMBERMODELCOMMENTS.' AS T1 JOIN '.configs::TABLE_MODEL." AS T2
						ON
							T1.M_CODE = T2.CODE
						WHERE
							T1.OWNER_ID = '$login_member_id'
						LIMIT
							$startIndex,$numPerPage;";
			return $con->query( $query );
		}
	}
?>